package com.filesystem;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.util.ArrayList;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author 作者：zhaosj
 * @version 创建时间：Sep 7, 2009 3:30:21 PM 遍历文件夹（递归）
 */

public class FileSystem1 {
	// private static ArrayList filelist = new ArrayList();

	public static void refreshFileList(String strPath) {
		File dir = new File(strPath);
		File[] files = dir.listFiles();

		// ApplicationContext ctx = new
		// ClassPathXmlApplicationContext("applicationContext.xml") ;
		Resource resource = new ClassPathResource("conf/applicationContext.xml");
		BeanFactory factory = new XmlBeanFactory(resource);
		JdbcTemplate jt = (JdbcTemplate) factory.getBean("jdbcTemplate");

		if (files == null)
			return;
		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()) {
				refreshFileList(files[i].getAbsolutePath());
			} else {
				String strFileName = files[i].getAbsolutePath().toLowerCase();
				System.out.println("---" + strFileName);
				// filelist.add(files[i].getAbsolutePath());
			}
		}
	}

	public static void bufferReader(String strPath) {

		try {
			File dir = new File(strPath);
			File[] files = dir.listFiles();

			ArrayList<String> al = new ArrayList<String>();
			String line = null;

			if (files == null)
				return;
			for (int i = 0; i < files.length; i++) {
				String strFileName = files[i].getAbsolutePath().toLowerCase();
				System.out.println("---" + strFileName);

				BufferedReader in = new BufferedReader(new FileReader(strFileName));
				while ((line = in.readLine()) != null && !"".equals(line)) {
					al.add(line);
					// System.out.println(line);
				}
				
				in.close();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void fileInputStream(String strPath) {
		try {
			File dir = new File(strPath);
			File[] files = dir.listFiles();

			ArrayList<String> al = new ArrayList<String>();
			//String line = null;

			if (files == null)
				return;
			for (int i = 0; i < files.length; i++) {
				String strFileName = files[i].getAbsolutePath().toLowerCase();
				System.out.println("---" + strFileName);

				FileInputStream in = new FileInputStream(new File(strFileName));
				// BufferedReader in = new BufferedReader(new FileReader(strFileName));
				while (in.read() != -1) {
					al.add(in.toString());
					// System.out.println(line);
				}
				
				in.close();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

		long a = System.currentTimeMillis();
		bufferReader("F:\\MYDOCUMENT\\_TEMP\\test");

		System.out.println(System.currentTimeMillis() - a + " ms ...");
	}

}
